/*
 * @[H]:  Copyright (c) 2021 Phytium Information Technology, Inc. 
 * 
 *  SPDX-License-Identifier: Apache-2.0. 
 * 
 * @Date: 2021-07-14 22:50:06
 * @LastEditTime: 2021-08-02 09:19:58
 * @Description:  Description of file
 * @Modify History: 
 * * * Ver   Who        Date         Changes
 * * ----- ------     --------    --------------------------------------
 */
#include "f_iomux.h"
#include "ft_assert.h"
#include "ft_debug.h"

#define IOMUX_DEBUG_TAG "IOMUX"

#define IOMUX_ERROR(format, ...) FT_DEBUG_PRINT_E(IOMUX_DEBUG_TAG, format, ##__VA_ARGS__)
#define IOMUX_DEBUG_I(format, ...) FT_DEBUG_PRINT_I(IOMUX_DEBUG_TAG, format, ##__VA_ARGS__)
#define IOMUX_DEBUG_W(format, ...) FT_DEBUG_PRINT_W(IOMUX_DEBUG_TAG, format, ##__VA_ARGS__)

typedef struct
{
    volatile FIOMUX_REG_TYPE regs[FIOMUX_REG_LENGTH];
} FIomux;

FIomux *iomux = (FIomux *)FIOMUX_REG_BASEADDR;

void FIomuxSetFun(int pad_offset, PadFun fun)
{

    int pad, pin, val;

    pad = pad_offset / 8;
    pin = pad_offset % 8;
    pin = 7 - pin;
    FT_ASSERTVOID(pad < FIOMUX_REG_LENGTH);

    val = iomux->regs[pad];
    val &= ~(0x3 << (pin * 4));
    val |= (fun << (pin * 4));
    iomux->regs[pad] = val;

    // IOMUX_DEBUG_I("addr:[0x%x] val:[0x%x] pin:[%d]\n", &iomux->regs[pad], iomux->regs[pad], pin);
}

void FIomuxSetPull(int pad_offset, PadPull pull)
{

    int pad, pin, val;

    pad = pad_offset / 8;
    pin = pad_offset % 8;
    pin = 7 - pin;
    FT_ASSERTVOID(pad < FIOMUX_REG_LENGTH);

    val = iomux->regs[pad];
    val &= ~(0x3 << (pin * 4 + 2));
    val |= (pull << (pin * 4 + 2));
    iomux->regs[pad] = val;

    // IOMUX_DEBUG_I("addr:[0x%x] val:[0x%x] pin:[%d]\n", &iomux->regs[pad], iomux->regs[pad], pin);
}